package com.lhh.userservice.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lhh.springcommon.annotation.WebLog;
import com.lhh.springcommon.entity.Collect;
import com.lhh.springcommon.entity.Follow;
import com.lhh.springcommon.entity.User;
import com.lhh.springcommon.entity.Video;
import com.lhh.springcommon.entity.vo.CountUserInfoVO;
import com.lhh.springcommon.interceptor.JwtInterceptor;
import com.lhh.springcommon.service.ICollectService;
import com.lhh.springcommon.service.IFollowService;
import com.lhh.springcommon.service.IUserService;
import com.lhh.springcommon.service.IVideoService;
import com.lhh.userservice.service.UserPublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 李厚航
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserPublishServiceImpl implements UserPublishService {

    private final IVideoService videoService;

    private final ICollectService collectService;

    private final IFollowService followService;

    private final IUserService userService;


    @Autowired
    public UserPublishServiceImpl(IVideoService videoService,
                                  ICollectService collectService,
                                  IFollowService followService,
                                  IUserService userService) {
        this.videoService = videoService;
        this.collectService = collectService;
        this.followService = followService;
        this.userService = userService;
    }


    @Override
    @WebLog
    public Long countPublish(Integer uid) {
        return videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getPublisherId, uid));
    }

    @Override
    @WebLog
    public CountUserInfoVO countInfoByUserId() {
        Integer id = JwtInterceptor.getUserIdFromThreadLocal();
        return new CountUserInfoVO(
                followService.count(new LambdaQueryWrapper<Follow>().eq(Follow::getUid, id)),
                followService.count(new LambdaQueryWrapper<Follow>().eq(Follow::getFid, id)),
                collectService.count(new LambdaQueryWrapper<Collect>().eq(Collect::getUid, id)),
                videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getPublisherId, id))
        );
    }

    @Override
    @WebLog
    @CacheEvict(value = "userToken",key = "T(com.lhh.springcommon.interceptor.JwtInterceptor).userIdFromThreadLocal")
    public Boolean updateUserInfo(User user) {
        return userService.updateById(user);
    }
}
